//
//  LoginVCHelper.swift
//  物料商行
//
//  Created by HaoDong Hong on 2017/8/13.
//  Copyright © 2017年 HaoDong Hong. All rights reserved.
//

import Foundation
import Alamofire
import AlamofireImage
import SwiftyJSON

enum LoginError: Error {
    case wrongPassword
    case wrongPhone
    case networkError
    
    //registe
    case userExist
    
    //forget
    case wrongMessage
    case userNotExist
}

class LoginVCHelper {
    let queue = DispatchQueue(label: "networking")
    
    func login(with phone: String, password: String, completeHandler: @escaping (LoginError?) -> Void) {
        let request = UserRequest.login(phone: phone, password: password)
        
        Alamofire.request(request.fullURL, method: .post, parameters: request.bodyParams, headers: nil).responseJSON(queue: queue) { (response) in
            guard let result = response.result.value else {
                debugPrint(response.error ?? "nil")
                DispatchQueue.main.async {
                    completeHandler(LoginError.networkError)
                }
                return
            }
            let json = JSON(result)
            if json["status"].int! == 0 {
                if json["errmsg"]["code"].int! == 101 {
                    DispatchQueue.main.async {
                        completeHandler(LoginError.wrongPassword)
                        return
                    }
                } else {
                    DispatchQueue.main.async {
                        completeHandler(LoginError.wrongPhone)
                        return
                    }
                }
            } else {
                User.sessionID = json["data"]["session_id"].string!
                User.phoneNumber = phone
                DispatchQueue.main.async {
                    completeHandler(nil)
                }
            }
            
        }
    }
    
    
    func sendMessage(with phone: String, completionHandler: @escaping ((Bool) -> Void)){
        let request = UserRequest.sendMessage(phone: phone)
        Alamofire.request(request.fullURL, method: .post, parameters: request.bodyParams, headers: nil).responseJSON(queue: queue) { (response) in
            guard response.error == nil else {
                DispatchQueue.main.async {
                    completionHandler(false)
                }
                return
            }
            DispatchQueue.main.async {
                completionHandler(true)
            }
        }
    }
    
    func register(with phone: String, password: String, completionHandler: @escaping ((LoginError?) -> Void)) {
        let request = UserRequest.registe(phone: phone, password: password)
        Alamofire.request(request.fullURL, method: .post, parameters: request.bodyParams).responseJSON(queue: queue) { (response) in
            guard let result = response.result.value else {
                DispatchQueue.main.async {
                    completionHandler(LoginError.networkError)
                }
                return
            }
            let json = JSON(result)
            if json["status"].intValue == 0 {
                    DispatchQueue.main.async {
                        completionHandler(LoginError.userExist)
                }
                return
            } else {
                DispatchQueue.main.async {
                    completionHandler(nil)
                }
                return
            }
        }
    }
    func forget(with phone: String, password: String, textMessage: String, completionHandler: @escaping ((LoginError?) -> Void )) {
        let request = UserRequest.forget(phone: phone, newPass: password, validateCode: textMessage)
        Alamofire.request(request.fullURL, method: .post, parameters: request.bodyParams, headers: nil).responseJSON(queue: queue) { (response) in
            guard let result = response.result.value else {
                DispatchQueue.main.async {
                    completionHandler(LoginError.networkError)
                }
                return
            }
            let json = JSON(result)
            if json["status"].int == 0 {
                if json["errmsg"]["code"] == 102 {
                    DispatchQueue.main.async {
                        completionHandler(LoginError.userNotExist)
                    }
                } else if json["errmsg"]["code"] == 105 {
                    DispatchQueue.main.async {
                        completionHandler(LoginError.wrongMessage)
                    }
                }
            } else {
                DispatchQueue.main.async {
                    completionHandler(nil)
                }
            }
        }
    }
    
}
